using System;
using System.Collections.Generic;
using CMS_Admin.Backend.Api.Entity;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using CMS_Admin.Backend.Api.ParamModel;
using Newtonsoft.Json;
using CMS_Admin.Backend.Api.Repository;
using CMS_Admin.Backend.Api.Utils;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Authorization;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;

namespace CMS_Admin.Backend.Controllers
{

    [ApiController]
    [Route("articleClass")]
    public class ArticleClassController : ControllerBase
    {

        private IRepository<ArticleClass> _articleClass;
        private readonly IConfiguration _configuration;

        public ArticleClassController(IConfiguration configuration, IRepository<ArticleClass> articleClass)
        {

            _articleClass = articleClass;
            _configuration =configuration;


        }


        /// <summary>
        /// 查找文章栏目
        /// </summary>
        /// <returns></returns>
        // [HttpGet]
        // public dynamic Get()
        // {

        //     var articleClass = _articleClass.Table.OrderByDescending(art => art.Id);
        //     var res = new
        //     {
        //         Code = 200,
        //         Data = articleClass,

        //         Msg = "获取栏目列表成功^_^"
        //     };
        //     return res;
        // }

        [HttpGet]
        public dynamic Get([FromQuery] Pager pager)
        {
            var pageIndex = pager.PageIndex;
            var pageSize = pager.PageSize;
            
            var articleClass = _articleClass.Table.OrderByDescending(art => art.Id).Where(a=>a.IsDeleted==false);
            
            // var c = comment.Count();
            // 对获得的记录分页
            var u = articleClass.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

            // // 返回的数据里，带有数据和分页信息
             var res = new
            {
                Code = 200,
                Data = articleClass,

                Msg = "获取栏目列表成功^_^"
            };
            return res;
        }



        /// <summary>
        /// 添加栏目
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>

        [HttpPost]
        public string Post(NewArticleClass model)
        {


            var articleClassName = model.ArticleClassName.Trim();
            var acn = _articleClass.Table.Where(a => a.ArticleClassName == articleClassName).FirstOrDefault();
            if (acn == null)
            {

                if (string.IsNullOrEmpty(articleClassName))
                {
                    var tmp = new
                    {
                        Code = 1004,
                        Data = "",
                        Msg = "文章栏目名称不能为空，请确认后重试"
                    };
                    return JsonHelper.Serialize(tmp);
                }


                var ac = new ArticleClass
                {
                    ArticleClassName = model.ArticleClassName

                };

                _articleClass.Insert(ac);

                var res = new
                {
                    Code = 200,
                    Data = ac,
                    Msg = "创建栏目成功"
                };
                return JsonHelper.Serialize(res);

            }

            else{

                var tmp = new
                    {
                        Code = 1004,
                        Data = "",
                        Msg = "栏目已存在"
                    };

                     return JsonHelper.Serialize(tmp);
            }


        }




        /// <summary>
        /// 真删除指定栏目
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {
            var ac = _articleClass.GetById(id);
            if (ac != null)
            {
                _articleClass.Delete(id);
                return new
                {
                    Code = 200,
                    Msg = string.Format("删除指定id为{0}的栏目成功", id)
                };
            }
            else
            {
                return new
                {
                    Code = 1004,
                    Data = "",
                    Msg = "指定Id栏目户不存在，请确认后重试"
                };
            }

        }






        /// <summary>
        /// 修改栏目
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <returns></returns>

        [HttpPut("{id}")]
        public dynamic Put(int id, NewArticleClass model)
        {
            var ac = _articleClass.GetById(id);

            if (ac != null)
            {
                // roles.RoleName = model.RoleName;
                
                ac.IsActived = model.IsActived;

                ac.IsDeleted = model.IsDeleted?true:false;
                

                ac.UpdatedTime = DateTime.Now;
                _articleClass.Update(ac);

                return JsonHelper.Serialize(new
                {
                    Code = 200,
                    Data = ac,
                    Msg = string.Format("你修改的栏目的id为：{0}，已经修改成功，请注意查收", id)
                });
            }
            else
            {
                return JsonHelper.Serialize(new
                {
                    Code = 1004,
                    Data = "",
                    Msg = "指定Id的栏目不存在，请确认后重试"
                });
            }
        }




    }


}